package com.alibaba.jvm.sandbox.module.mgr;

import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.Resource;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.ConfigInfo;
import org.kohsuke.MetaInfServices;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 沙箱信息模块
 */
@MetaInfServices(Module.class)
@Information(id = "sandbox-info", version = "0.0.1", author = "xule")
public class InfoModule implements Module {

    @Resource
    private ConfigInfo configInfo;

    @Command("version")
    public void version(final PrintWriter writer) throws IOException {
        // http://127.0.0.1:64099/sandbox/default/module/http/rasp-info/version
        // 输出为json格式
        final StringBuilder versionSB = new StringBuilder()
                .append("{")
                .append("\"namespace\":\"").append(configInfo.getNamespace()).append("\",")
                .append("\"raspVersion\":\"").append(configInfo.getVersion()).append("\",")
                .append("\"agentMode\":\"").append(configInfo.getMode()).append("\",")
                .append("\"hostName\":\"").append(configInfo.getServerAddress().getHostName()).append("\",")
                .append("\"port\":\"").append(configInfo.getServerAddress().getPort()).append("\",")
                .append("\"raspHome\":\"").append(configInfo.getHome()).append("\",")
                .append("\"systemModuleLib\":\"").append(configInfo.getSystemModuleLibPath()).append("\",")
                .append("\"userModuleLib\":\"").append(configInfo.getUserModuleLibPath()).append("\",")
                .append("\"systemProviderLib\":\"").append(configInfo.getSystemProviderLibPath()).append("\"")
                .append("}");
        writer.println(versionSB);
        writer.flush();
    }
}
